package com.xd.common.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.HashMap;
import java.util.Map;
@Configuration
public class RabbitConfig {
    public static final String DELAY_EXCHANGE = "delay.exchange";//延迟交换机
    public static final String DELAY_QUEUE = "delay.queue";//延迟队列
    public static final String DELAY_QUEUE_SIGNPAY = "delay.queueSignPay";//代扣申请失败延迟队列
    public static final String DELAY_QUEUE_CHECKINCOMING  = "delay.queueCheckIncoming";//审核进件延迟队列
    public static final String DELAY_ROUTING_KEY = "delay.routing";//延迟路由
    public static final String DELAY_ROUTING_KEY_SIGNPAY = "delay.routingSignPay";//代扣申请失败延迟路由
    public static final String DELAY_ROUTING_KEY_CHECKINCOMING = "delay.CheckIncoming";//自动审核延迟路由

    public static final String CERTIFICATION_EXCHANGE = "certification.exchange";   // 普通交换机
    public static final String CERTIFICATION_QUEUE = "certification.queue";         // 公证失败
    public static final String CERTIFICATION_ROUTING_KEY = "certification.fail"; // 公证失败 key

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }
    @Bean
    public DirectExchange certificationExchange() {
        return new DirectExchange(CERTIFICATION_EXCHANGE, true, false);
    }
    @Bean
    public Queue certificationQueue() {
        return new Queue(CERTIFICATION_QUEUE, true);
    }
    @Bean
    public Binding normalBinding(Queue certificationQueue, DirectExchange normalExchange) {
        return BindingBuilder.bind(certificationQueue).to(normalExchange).with(CERTIFICATION_ROUTING_KEY);
    }
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");

        return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue delayQueue() {
        return new Queue(DELAY_QUEUE, true);
    }
    @Bean
    public Queue delayQueueSignPay() {
        return new Queue(DELAY_QUEUE_SIGNPAY, true);
    }
    @Bean
    public Queue delayCheckIncomingQueue() {
        return new Queue(DELAY_QUEUE_CHECKINCOMING, true);
    }

    @Bean
    public Binding delayBinding(Queue delayQueue, CustomExchange delayExchange) {
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(DELAY_ROUTING_KEY).noargs();
    }
    @Bean
    public Binding delaySignPayBinding(Queue delayQueueSignPay, CustomExchange delayExchange) {
        return BindingBuilder.bind(delayQueueSignPay).to(delayExchange).with(DELAY_ROUTING_KEY_SIGNPAY).noargs();
    }
    @Bean
    public Binding delayCheckIncomingBinding(Queue delayCheckIncomingQueue, CustomExchange delayExchange) {
        return BindingBuilder.bind(delayCheckIncomingQueue).to(delayExchange).with(DELAY_ROUTING_KEY_CHECKINCOMING).noargs();
    }

}
